package com.hotel.homehotel.before.controller;

import com.hotel.homehotel.before.entiy.User;
import com.hotel.homehotel.before.service.UploadService;
import com.hotel.homehotel.before.service.UserService;
import com.hotel.homehotel.utils.ConversionUtils;
import com.hotel.homehotel.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags="用户管理" ,value="用户管理")
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UploadService uploadService;
//    @ApiOperation(value = "登录")
//    @PostMapping("/login/{phoneNumber}/{password}")
//    public R login( @ApiParam(name="phoneNumber",value = "手机号",required = true)
//                    @PathVariable String phoneNumber,  @ApiParam(name="password",value = "密码",required = true)
//                    @PathVariable String password){
//        //验证用户
//        User user = userService.checkLogin(phoneNumber,password);
//        if(user!=null )
//        {
//            String token=  JwtUtils.getJwtToken(user.getPhoneNumber());//传电话过去生成唯一的Token值
//
//            return  R.define(true,200,"登录成功").data("token",token).data("user", ConversionUtils.toUserVo(user));
//        }
//        return  R.define(false,400,"登录失败").data("user",null);
//    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public R login(@RequestBody  User user){
        //验证用户
        boolean bool= userService.register(user);
        if(bool)
            return  R.define(true,200,"注册成功").data("user",bool);
        return R.define(false,400,"注册失败").data("user",bool);
    }

    @ApiOperation(value = "通过手机号获取用户信息")
    @GetMapping("/getInfoByPhoneNumber/{phoneNumber}")
    public R login(@ApiParam(name="phoneNumber",value = "手机号",required = true)
                   @PathVariable String phoneNumber){
        User user = userService.getInfo(phoneNumber);
        if(user!=null )
        {
            return  R.define(true,200,"获取成功").data("user", ConversionUtils.toUserVo(user));
        }
        return  R.define(false,400,"获取失败").data("user",null);
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping("/userUpdate")
    public R userUpdate(@RequestBody User user)
    {
        boolean bool = userService.userUpdate(user);
        if(bool)
            return  R.define(true,200,"修改修改成功");
        return  R.define(false,400,"修改失败");

    }

    @ApiOperation(value = "删除用户信息")
    @GetMapping("/userDelete/{id}")
    public R userDelete(@ApiParam(name="id",value = "id",required = true)
                            @PathVariable String id){
        boolean bool = userService.userDelete(id);
        if(bool)
            return  R.define(true,200,"删除成功");
        return  R.define(false,400,"删除失败");
    }


    @ApiOperation(value = "文件上传")
    @PostMapping("/ImgUpload")
    public R fileUpload(@ApiParam(name = "file", value = "图片", required = true)
                                MultipartFile file) {
        String upload = uploadService.upload(file);
        return  R.define(true,200,"上传成功").data("url",upload);
    }
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value = "查询所有用户")
    @PostMapping("/getAllUser")
    public R getAllUser(){

        return  R.define(true,200,"查询成功").data("users",userService.getAllUser());
    }
}
